package org.jackysoft.struts2.fileserver;

import java.io.ByteArrayInputStream;
import java.util.Calendar;
import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.annotations.Logger;
import org.jackysoft.entity.FileItem;
import org.jackysoft.entity.Folder;
import org.jackysoft.service.DownloadService;
import org.jackysoft.service.FileDataService;
import org.jackysoft.service.TranslateService;
import org.jackysoft.struts2.UtilsCRUDAction;
import org.jackysoft.util.DateUtils;
import org.jackysoft.util.FileInfo;
import org.jackysoft.util.HttpUtil;
import org.jackysoft.util.OperationType;
import org.jackysoft.util.StringUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.util.CollectionUtils;


@Namespace("/fileserver/file")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Logger(label="传递文件",logType=OperationType.User)
public class TranslateFileAction extends UtilsCRUDAction<FileItem> {

	private static final long serialVersionUID = 7371987998449733980L;
	protected TranslateService translateService;
	private DownloadService service;
	protected FileDataService fileService;	
	private Folder folder;
	protected Collection<String> files;
	protected String fileId;	
	protected Collection<String> tousers;
	protected String fileType;
	protected Calendar updateDate;
	protected FileItem fileItem;
	protected FileItem[] fileItems;
	private String fromUrl;


	public String translate() throws Exception {
		translateService.translate(fileType, getFiles(), tousers);
		return SUCCESS;
	}


	public String changefolder() throws Exception {
		translateService.changeDir(getFiles(), getFolder().getId());
		return SUCCESS;
	}
	

	public String cleanup()throws Exception{
		int year = DateUtils.getYear(updateDate);
		int month = DateUtils.getMonth(updateDate);
		fileService.clear(year, month);
		return SUCCESS;
	}


	/**
	 * 打开一个office格式的文件用来提供用户编辑
	 * 目前只支持Word文档
	 * **/
	public String opendoc() throws Exception{
		FileInfo rm = getService().download(fileId);
		upLoadFileName = StringUtils.toDownloadFileName(rm.getTitle()+"."+rm.getFileType());
		contentLength = rm.getData().length;
		contentType = HttpUtil.extToMimeType(rm.getFileType());
		inputStream = new ByteArrayInputStream(rm.getData());
		return STREAM;
	}
	
	public String openforedit() throws Exception{
		fileItem = fileService.getForEdit(fileId);
		
		return "openforedit";
	}
	
	
	public String removebyid() throws Exception {
		fileService.removeByIdz(getFiles());
		return SUCCESS;
	}

	
	public String userselector() throws Exception{
		if(CollectionUtils.isEmpty(getFiles()))
			return "filenotselect";
		setFileItems(fileService.getsAllByID(getFiles(),fileType));
		return "userselector";
	}
	
	
	
	@Resource
	public void setTranslateService(TranslateService translateService) {
		this.translateService = translateService;
	}

	public TranslateService getTranslateService() {
		return translateService;
	}
	

	public Collection<String> getTousers() {
		return tousers;
	}

	public FileDataService getFileService() {
		return fileService;
	}




	public void setTousers(Collection<String> tousers) {
		this.tousers = tousers;
	}

	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	
	
	@Resource
	public void setFileService(FileDataService fileService) {
		this.setDataProvider(fileService);
		this.fileService = fileService;
	}

	public void setUpdateDate(Calendar updateDate) {
		this.updateDate = updateDate;
	}

	public Calendar getUpdateDate() {
		return updateDate;
	}


	public String getFileId() {
		return fileId;
	}


	public void setFileId(String fileId) {
		this.fileId = fileId;
	}


	public FileItem getFileItem() {
		return fileItem;
	}


	public void setFileItem(FileItem fileItem) {
		this.fileItem = fileItem;
	}


	public Collection<String> getFiles() {
		return files;
	}


	public void setFiles(Collection<String> files) {
		this.files = files;
	}


	public FileItem[] getFileItems() {
		return fileItems;
	}


	public void setFileItems(FileItem[] fileItems) {
		this.fileItems = fileItems;
	}


	public String getFromUrl() {
		return fromUrl;
	}


	public void setFromUrl(String fromUrl) {
		this.fromUrl = fromUrl;
	}


	public Folder getFolder() {
		return folder;
	}


	public void setFolder(Folder folder) {
		this.folder = folder;
	}


	public DownloadService getService() {
		return service;
	}


	@Resource
	public void setService(DownloadService service) {
		this.service = service;
	}
}
